package com.xl.juc.collection.unsafe;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * hashSet本质 ：HashMap
 * public HashSet() {
   map = new HashMap<>();
 }

 add方法：就是map的可以 value PRESENT是一个常量
 public boolean add(E e) {
 return map.put(e, PRESENT)==null;
 }
 * @author Charley
 * @create 2020-12-27
 */
public class SetTest {
    public static void main(String[] args) {
        Set<String> set =new CopyOnWriteArraySet<>();
        //Exception in thread "Thread-60" java.util.ConcurrentModificationException
        /**
         * 解决方案
         * 1.  Set<String> set = Collections.synchronizedSet(new HashSet<>());
         * 2.  Set<String> set =new CopyOnWriteArraySet<>();
         */
        for (int i = 0; i <100 ; i++) {
            new Thread(()->{
                set.add(UUID.randomUUID().toString().substring(0,5));
                System.out.println(set);
            }).start();
        }
    }
}
